﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using Common;
using DataTool;
using System.Data;
namespace OneGateWF.ProcessContestNS
{
    public class wait4Grade : NativeActivity
    {
        [RequiredArgument]
        public InArgument<string> Judge { get; set; }

        [RequiredArgument]
        public InArgument<SUBMISSION> sub { get; set; }
        protected override void Execute(NativeActivityContext context)
        {
            context.CreateBookmark("SF_"+Judge.Get(context)+"_"+sub.Get(context).Id, new BookmarkCallback(OnReadComplete));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="bookmark"></param>
        /// <param name="state"></param>
        void OnReadComplete(NativeActivityContext context, Bookmark bookmark, object state)
        {
            var ss = (SubmissionGradeSession)state;
            wfdbEntities db = new wfdbEntities();
            sub.Get(context).SubmissionFromDb(db).SUBMISSIONGRADEs.Add
                (
                    new SUBMISSIONGRADE
                    {
                        Grade = ss.Grade,
                        Username = ss.Username
                    }
                );
            db.SaveChanges();                
        }

        /// <summary>
        /// 
        /// </summary>
        protected override bool CanInduceIdle
        {
            get
            {
                return true;
            }
        }
    }

    public class CalculateSubmissionGrade : CodeActivity
    {
        [RequiredArgument]
        public InArgument<SUBMISSION> sub { get; set; }
        protected override void Execute(CodeActivityContext context)
        {
            wfdbEntities db = new wfdbEntities();
            float f = 0;
            int time = 0;
            var submis = sub.Get(context).SubmissionFromDb(db);
            foreach (var item in submis.SUBMISSIONGRADEs)
            {
                f += item.Grade.Value;
                time++;
            }

            f = f / time;
            Math.Round(f, digits: 1);
            submis.FinalGrade = f;
            db.SaveChanges();
        }        
    }

    public class CreateContest : CodeActivity
    {
        [RequiredArgument]
        public InArgument<CONTEST> contest { get; set; }
        protected override void Execute(CodeActivityContext context)
        {
            var c = contest.Get(context);
            c.Status = 1;
            c.CreateDate = DateTime.Now;
            wfdbEntities db = new wfdbEntities();
            db.CONTESTs.AddObject(c);
            db.SaveChanges();
            foreach (string s in c.Judges.Split(';'))
            {
                ContestNotifyClass n = new ContestNotifyClass
                {
                    Cont = "Bạn đã được mời làm giám khảo của cuộc thi \"<a href='../Contest/Detail/"+c.Id+"'>" + c.Title + "</a>\"",
                    Username = s,
                };
                n.NewNotify();
            }
            NEWS ns = new NEWS();
            ns.Date = DateTime.Now;
            ns.Id = Guid.NewGuid();
            ns.Title = "Phát động cuộc thi <a href='../Contest/Detail/" + c.Id + "'>" + c.Title + @" </a> . Bắt đầu nhận bài thi của sinh viên từ hôm nay";
            db.NEWS.AddObject(ns);
            db.SaveChanges();
        }        
    }

    public class Wait4Contestant : NativeActivity<bool>
    {
        [RequiredArgument]
        public InArgument<CONTEST> contest { get; set; }
        protected override void Execute(NativeActivityContext context)
        {
            context.CreateBookmark("Wait4Contestant", new BookmarkCallback(OnReadComplete));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="bookmark"></param>
        /// <param name="state"></param>
        void OnReadComplete(NativeActivityContext context, Bookmark bookmark, object state)
        {
            wfdbEntities db = new wfdbEntities();
            var c = contest.Get(context).ContestFromDb(db);
            if (state.GetType() == Type.GetType(typeof(StopAcceptContestantSession).AssemblyQualifiedName))
            {
                context.SetValue(Result, false);
            }
            else
            {
                context.SetValue(Result, true);
                var sub = ((SendSubmissionSession)state);                
                c.SUBMISSIONs.Add(sub.sub);
                
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 
        /// </summary>
        protected override bool CanInduceIdle
        {
            get
            {
                return true;
            }
        }
    }

    public class SetContestStatus : CodeActivity
    {
        [RequiredArgument]
        public InArgument<CONTEST> contest { get; set; }
        [RequiredArgument]
        public InArgument<byte> status { get; set; }
        protected override void Execute(CodeActivityContext context)
        {
            wfdbEntities db = new wfdbEntities();
            var c = contest.Get(context).ContestFromDb(db);
            c.Status = status.Get(context);
            db.SaveChanges();
        }
    }

    public class NotifyJudgesToGrade : CodeActivity
    {
        [RequiredArgument]
        public InArgument<CONTEST> contest { get; set; }
        protected override void Execute(CodeActivityContext context)
        {
            var c = contest.Get(context);
            foreach (string s in c.Judges.Split(';'))
            {
                ContestNotifyClass n = new ContestNotifyClass
                {
                    Cont = "cuộc thi \"<a href='../Contest/Detail/" + c.Id + "'>" + c.Title + "</a>\" Đã bước vào giai đoạn chấm điểm, bạn là giám khảo và có thể bắt đầu chấm điểm bài thi <a href='../Contest/PostListFromContest/"+c.Id+"'>" + "Vào đây để chấm điểm" + "</a>",
                    Username = s,
                };
                n.NewNotify();
            }
            NEWS ns = new NEWS();
            ns.Date = DateTime.Now;
            ns.Id = Guid.NewGuid();
            ns.Title = "cuộc thi <a href='../Contest/Detail/" + c.Id + "'>" + c.Title + " </a>" + " Đã bước vào giai đoạn chấm điểm, Dừng nhận bài thi từ bây giờ";
            ns.Publish();
        } 
    }

    public class PopulateResult : CodeActivity
    {
        [RequiredArgument]
        public InArgument<CONTEST> contest { get; set; }
        protected override void Execute(CodeActivityContext context)
        {
            wfdbEntities db = new wfdbEntities();
            var c = contest.Get(context).ContestFromDb(db);
            c.Status = 3;
            NEWS n = new NEWS(); 
            n.Date = DateTime.Now;
            n.Id = Guid.NewGuid();
            n.Title = @"cuộc thi <a href='../Contest/Detail/" + c.Id + "'>" + c.Title + @" </a> Đã có kết quả. Bấm vào link để biết thêm chi tiết";
            db.NEWS.AddObject(n);
            db.SaveChanges();
            //context.CreateBookmark("Wait4Result" + Judge.Get(context), new BookmarkCallback(OnReadComplete));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="bookmark"></param>
        /// <param name="state"></param>
        void OnReadComplete(NativeActivityContext context, Bookmark bookmark, object state)
        {
        }

        /// <summary>
        /// 
        /// </summary>

    }

}
